<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :search.sync="search"
      @row-del="rowDel"
      :before-open="beforeOpen"
      :page="page"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menuRight="{ row }">
        <el-button type="primary" size="small" @click="addRow"
          >新增优惠券</el-button
        >
      </template>
      <template #menuLeft>
        <el-input
          placeholder="请输入活动名称"
          size="small"
          style="width: 250px"
          suffix-icon="el-icon-search"
        />
      </template>
      <template #couponStatus="{ row }">
        <div>
          <span
            v-if="
              new Date(row.endTime).getTime() >= new Date().getTime() &&
              new Date().getTime() >= new Date(row.beginTime).getTime()
            "
            >进行中</span
          >
          <span v-if="new Date().getTime() < new Date(row.beginTime).getTime()"
            >未开始</span
          >
          <span
            v-if="new Date(row.endTime).getTime() < new Date().getTime()"
            class="c-red"
            >已过期</span
          >
        </div>
      </template>
    </avue-crud>
    <el-dialog
      title="新增优惠券"
      append-to-body
      :visible.sync="addDialog"
      width="600px"
    >
      <apostle-rebate-add
        @closeAddDialog="closeAddDialog"
        @openMachineChange="openMachineChange"
        :selectedMachine="selectedMachine"
        @saveRefresh="saveRefresh"
      />
    </el-dialog>
    <el-dialog
      :before-close="handleMachines"
      title="选择加注机"
      append-to-body
      :visible.sync="chooseMachineDialog"
      width="600px"
    >
      <selected-filling-machine-text-button @change="machineChange">
      </selected-filling-machine-text-button>
    </el-dialog>
  </basic-container>
</template>

<script>
import apostleRebateAdd from "@/views/marketing-management/apostle-rebate-add.vue";
import { getList, remove } from "@/api/marketing-management/apostle-rebate";
import { mapGetters } from "vuex";
import SelectedFillingMachineTextButton from "@/components/business/selected-filling-machine-text-button.vue";
import { msgError } from "@/util/common-util";

export default {
  data() {
    return {
      form: {},
      search: {},
      roleBox: false,
      excelBox: false,
      loading: true,
      selectionList: [],
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      props: {
        label: "title",
        value: "key",
      },
      option: {
        columnBtn: false,
        refreshBtn: false,
        height: "auto",
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        selection: false,
        viewBtn: true,
        editBtn: false,
        addBtn: false,
        dialogWidth: "800px",
        labelWidth: "150px",
        align: "center",
        column: [
          {
            label: "活动名称",
            prop: "couponName",
            span: 24,
            disabled: true,
          },
          {
            label: "券面值（元）",
            prop: "couponAmount",
            width: 130,
            span: 24,
            disabled: true,
          },
          {
            label: "使用门槛（元）",
            prop: "couponOrderAmount",
            width: 150,
            span: 24,
          },
          {
            label: "领取方式",
            prop: "drawType",
            width: 140,
            overHidden: true,
            type: "select",
            dicData: [
              { label: "无条件领取", value: 0 },
              { label: "加注完成可领取", value: 1 },
            ],
            span: 24,
          },
          {
            label: "每人限领",
            prop: "couponLimit",
            span: 24,
          },
          {
            label: "发行数量",
            prop: "dispatchNum",
            span: 24,
          },
          {
            label: "开始时间",
            prop: "beginTime",
            type: "datetime",
            format: "yyyy-MM-dd",
            width: 120,
            span: 24,
          },
          {
            label: "结束时间",
            prop: "endTime",
            type: "datetime",
            format: "yyyy-MM-dd",
            width: 120,
            span: 24,
          },
          {
            label: "可用加注机",
            prop: "isAll",
            width: 120,
            slot: true,
            span: 24,
          },
          {
            label: "活动状态",
            prop: "couponStatus",
            slot: true,
            span: 24,
          },
        ],
      },
      data: [],
      addDialog: false,
      chooseMachineDialog: false,
      selectedMachine: [],
    };
  },
  components: {
    SelectedFillingMachineTextButton,
    apostleRebateAdd,
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
  },
  methods: {
    machineChange(list) {
      this.selectedMachine = list;
    },
    handleMachines(done) {
      if (!this.selectedMachine.length) msgError("请选择加注机");
      if (this.selectedMachine.length) {
        done();
      }
    },
    openMachineChange() {
      this.chooseMachineDialog = true;
    },
    saveRefresh() {
      this.addDialog = false;
      this.onLoad(this.page);
    },
    closeAddDialog() {
      this.addDialog = false;
    },
    addRow() {
      this.addDialog = true;
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        done();
      }
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
  },
};
</script>

<style></style>
